package com.ruoyi.park.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.park.domain.BuiPublicArea;
import com.ruoyi.park.mapper.BuiPublicAreaMapper;
import com.ruoyi.park.service.IBuiPublicAreaService;
import com.ruoyi.system.api.LinkFileServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 公共区域Service业务层处理
 *
 * @author cjie
 * @date 2021-07-20
 */
@Service
@RequiredArgsConstructor
public class BuiPublicAreaServiceImpl extends LinkFileServiceImpl implements IBuiPublicAreaService {
    private final BuiPublicAreaMapper buiPublicAreaMapper;

    /**
     * 查询公共区域
     *
     * @param id 公共区域ID
     * @return 公共区域
     */
    @Override
    public BuiPublicArea selectBuiPublicAreaById(String id) {
        BuiPublicArea buiPublicArea = buiPublicAreaMapper.selectBuiPublicAreaById(id);
        buiPublicArea.setFileList(getFileList(id));
        return buiPublicArea;
    }

    /**
     * 查询公共区域列表
     *
     * @param buiPublicArea 公共区域
     * @return 公共区域
     */
    @Override
    public List<BuiPublicArea> selectBuiPublicAreaList(BuiPublicArea buiPublicArea) {
        return buiPublicAreaMapper.selectBuiPublicAreaList(buiPublicArea);
    }

    /**
     * 新增公共区域
     *
     * @param buiPublicArea 公共区域
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBuiPublicArea(BuiPublicArea buiPublicArea) {
        buiPublicArea.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        buiPublicArea.setCreateTime(DateUtils.getNowDate());
        buiPublicArea.setCreateBy(SecurityUtils.getUsername());

        linkFile(buiPublicArea.getFileIdList(), buiPublicArea.getId());
        return buiPublicAreaMapper.insertBuiPublicArea(buiPublicArea);
    }

    /**
     * 修改公共区域
     *
     * @param buiPublicArea 公共区域
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateBuiPublicArea(BuiPublicArea buiPublicArea) {
        buiPublicArea.setUpdateTime(DateUtils.getNowDate());
        buiPublicArea.setUpdateBy(SecurityUtils.getUsername());

        linkFile(buiPublicArea.getFileIdList(), buiPublicArea.getId());
        return buiPublicAreaMapper.updateBuiPublicArea(buiPublicArea);
    }

    /**
     * 批量删除公共区域
     *
     * @param ids 需要删除的公共区域ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteBuiPublicAreaByIds(String[] ids) {
        deleteLinkFile(ids);
        return buiPublicAreaMapper.deleteBuiPublicAreaByIds(ids);
    }

    /**
     * 删除公共区域信息
     *
     * @param id 公共区域ID
     * @return 结果
     */
    @Override
    public int deleteBuiPublicAreaById(String id) {
        return buiPublicAreaMapper.deleteBuiPublicAreaById(id);
    }
}
